package com.platform.controller;

import com.platform.cache.J2CacheUtils;
import com.platform.entity.UserEntity;
import com.platform.entity.excel.UserExcel;
import com.platform.service.UserService;
import com.platform.util.Constants;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 会员管理:用户管理
 *
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserEntity> userList = userService.queryList(query);
        int total = userService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("user:info")
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.queryObject(id);

        return R.ok().put("user", user);
    }

    /**
     * 使用手机号用户查看信息
     */
    @RequestMapping("/infobyphone/{phone}")
    @RequiresPermissions("user:info")
    public R infobyphone(@PathVariable("phone") String phone) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("mobile", phone);
        List<UserEntity> userList = userService.queryList(param);
        UserEntity user = null;
        if (userList != null && userList.size() > 0)
            user = userList.get(0);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("user:save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateDone/{id}")
    @RequiresPermissions("user:update")
    public R update(@PathVariable("id") Long id) {
        int result = userService.update(id);
        if (result > 0) {
            J2CacheUtils.remove(Constants.REDIS_USER_CACHE, Constants.REDIS_USER_KEY + id);
            return R.ok();
        }
        return R.error("变更失败");

    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("user:update")
    public R updateinfo(@RequestBody UserEntity tPartner) {
        int result = userService.updateinfo(tPartner);
        if (result > 0) {
            J2CacheUtils.remove(Constants.REDIS_USER_CACHE, Constants.REDIS_USER_KEY + tPartner.getId());
            return R.ok();
        }
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/clear")
    public R clear() {
            J2CacheUtils.clear(Constants.REDIS_USER_CACHE);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("user:delete")
    public R delete(@RequestBody Integer[] ids) {
        userService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserEntity> userList = userService.queryList(params);

        return R.ok().put("list", userList);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = userService.queryTotal(params);

        return R.ok().put("userSum", sum);
    }
    /**
     * 总计
     */
    @RequestMapping("/delData")
    @RequiresPermissions("user:delete")
    public R deleteUserData(@RequestParam("uid") Integer uid) {

        int sum = userService.deleteUserData(uid);

        return R.ok();
    }

    /**
     * 导出会员
     */
    @RequestMapping("/export")
    @RequiresPermissions("user:export")
    public R export(@RequestParam(value = "id", required = false) String id,
                    @RequestParam(value = "name", required = false) String name,
                    @RequestParam(value = "phone", required = false) String phone,
                    @RequestParam(value = "done", required = false) Integer done,
                    @RequestParam(value = "frozen", required = false) Integer frozen,
                    @RequestParam(value = "index",required = false) Integer index,
                    @RequestParam(value = "size",required = false) Integer size,
                    HttpServletResponse response) {

        List<UserExcel> userList = userService.queryListExcel(id,name,phone,done,frozen,index,size);

        ExcelExport ee = new ExcelExport("用户管理");

        String[] header = new String[]{"用户编号", "手机号", "是否D-one", "注册时间"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userList != null && userList.size() != 0) {
            for (UserExcel userExcel : userList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("id", userExcel.getId());
                map.put("phone", userExcel.getMobile());
                map.put("done", userExcel.getDone());
                map.put("date", userExcel.getCreateTime());
                list.add(map);
            }
        }
        ee.addSheetByMap("用户管理", list, header);
        ee.export(response);
        return R.ok();
    }
}
